const util = require("../../utils/util")
import wxParse from '../../wxParse/wxParse.js'; // 富文本插件
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aid: 1, // 文档ID
    typeid: 0, // 栏目ID
    detail: {}, // 文档详情 
    imgPath: "/images",
    noCollectImage: 'http://qnstatic.lianhequnmei.com/images/20221221/collection.png',
    hasCollectImage: 'http://qnstatic.lianhequnmei.com/images/20221221/oncollection.png',
    collectImage: 'http://qnstatic.lianhequnmei.com/images/20221221/collection.png',
    status:0    // 是否收藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    if (options.aid !== 'undefined') {
      _this.data.aid = !options.aid ? 1 : options.aid;
    }
    if (options.typeid !== 'undefined') {
      _this.data.typeid = !options.typeid ? 1 : options.typeid;
    }
   
    _this.setData({
      aid: _this.data.aid,
      typeid: _this.data.typeid,
      imgPath:util.getImgPath()
    });
    _this.getPageData(); // 获取页面数据

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let _this = this;
    _this.getPageData(); // 获取页面数据
    wx.stopPullDownRefresh() // 停止下拉刷新
  },
  /**
   * 获取页面数据
   */
  getPageData() {
    this.getArchivesView(); // 获取文档详情
  },
  /**
   * 获取文档详情
   */
  getArchivesView() {
    let _this = this;
    const api = require("../../api/api")
    api.news.newDetail({
      id: _this.data.aid,
      success(res) {
        let data = res.data.data;
        if (res.data.code == 0) {
          if (data.content.length > 0) {
            wxParse.wxParse('content', 'html', data.content, _this, 0);
          }
          wx.setNavigationBarTitle({
            title: '家庭育儿指南' || '内容详情'
          })
          _this.setData({
            detail: data,
          });
          //是否已经收藏
          if (data.is_collection == 1) {
            _this.setData({
              collectImage: _this.data.hasCollectImage,
              status:2
            });
          } else {
            _this.setData({
              collectImage: _this.data.noCollectImage,
              status:1
            });
          }
        }
      }
    })
  },
  //添加或是取消收藏
  addCollectOrNot: function () {
    let that = this;
    const api = require("../../api/api")
    api.news.collection({
      article_id: that.data.aid,
      status:that.data.status,
      success(res) {
       let data  =  res.data;
       if (data.code == 0) {
        if (data.data.type == 'add') {
          that.setData({
            collectImage: that.data.hasCollectImage
          });
        } else {
          that.setData({
            collectImage: that.data.noCollectImage
          });
        }
        that.getPageData();
      } else {
        wx.showToast({
          title: res.data.msg,
          mask: true
        });
      }
       }
      })

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title:'家庭育儿指南',
     // path:'/pages/parenting/article?id='+this.data.aid,
     path:'/pages/index/index?query=wenzhang&aid='+this.data.aid,
      success: function (res) { 
          wx.showShareMenu({
            withShareTicket: true,
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              console.log(res)
            }
          })
      },
      fail: function (res) {
        console.log("转发失败:");
      }
    };
  }
})